<template>
  <div class="layout" @click.stop="aotuTab=false">
    <div class="header">
      <el-button size="mini" type="primary" @click="handleAdd">{{ $t('btn.save') }}</el-button>
    </div>
    <el-tabs v-model="essential">
      <el-tab-pane :label="$t('market.info')" name="information">
        <el-form ref="purchaseRef" :model="optionValue" label-width="100px" size="mini">
          <el-form-item :label="$t('market.billNo')">
            <el-input v-model="optionValue.fbillno" disabled />
          </el-form-item>
          <el-form-item :label="$t('market.orderDate')">
            <el-date-picker
              v-model.trim="optionValue.fdate"
              type="date"
              style="width: 152px"
              value-format="yyyy-MM-dd"
            />
          </el-form-item>
          <el-form-item :label="$t('market.note')" style="width: 50%;">
            <el-input v-model.trim="optionValue.fremark" style="width: 300px" />
          </el-form-item>
        </el-form>
        <div>
          <div style="margin-bottom: 5px">
            <el-button size="mini" @click="twoLine(1)">{{ $t('btn.newHang') }}<!--新增行--></el-button>
            <el-button size="mini" @click="twoLine(2)">{{ $t('btn.delHang') }}<!--删除行--></el-button>
            <el-button size="mini" @click.native="batchFill">{{ $t('btn.batchFill') }}<!--批量填充--></el-button>
          </div>
          <jc-table
            :table-data="tableData"
            :table-header="tableHeader"
            :table-height="tableHeight"
            serial
            :get-summaries="getSummaries"
            show-summary
            @rowClick="rowClick"
            @cellClick="cellClick"
          >
            <!--  物料编码  -->
            <template v-slot:billSlot="col">
              <el-input
                v-model.trim="col.scope.row.materialfnumber"
                size="mini"
                @input="getAtuo(col.scope.$index, col.scope.row.materialfnumber)"
              >
                <i
                  slot="suffix"
                  class="el-input__icon el-icon-search"
                  @click="handleGetMateriel(col.scope.row, col.scope.$index)"
                />
              </el-input>
              <div v-if="aotuTab && indexSelf === col.scope.$index" :class="indexSelf<7 ? 'tabPosit': 'tabPosit1'">
                <number-code :fnumber="fnumber" @material="materialData" />
              </div>
            </template>
            <!--  数量  -->
            <template v-slot:tagSlot="col">
              <el-input-number v-model="col.scope.row.fqty" :min="0" :max="col.scope.row.fcodeQty" size="mini" />
            </template>
            <!--  出库条码  -->
            <template v-slot:btnState="col">
              <el-select v-model="col.scope.row.foutcode" size="mini" @change="handelCode(col.scope.$index)">
                <el-option v-for="ite in col.scope.row.codes" :key="ite.fid" :label="ite.positionfnumber" :value="ite.fnumber" />
              </el-select>
            </template>
            <!--  入库条码  -->
            <template v-slot:btnStates="col">
              <el-input v-model.trim="col.scope.row.positionnumber" size="mini">
                <i
                  slot="suffix"
                  class="el-input__icon el-icon-search"
                  @click="handlePosition(col.scope.$index)"
                />
              </el-input>
            </template>
          </jc-table>
        </div>
      </el-tab-pane>
    </el-tabs>
    <!--物料弹窗-->
    <material v-if="isMateria" @material="materialData" />
    <positions v-if="isPositions" @client="positionData" />
  </div>
</template>

<script>
import autocomplete from '@/views/market/marketManage/createMarkerOrder/components/autocomplete'
import bility from '@/mixin/Operability'
import {
  insertTransfer
} from '@/api/wms/stock'
import { queryTBdCodeListByFnumber } from '@/api/repertory/picking/otherDelivery'
import positions from '@/views/wms/barcode/component/positions'

export default {
  name: 'OtherWarehousingAdd',
  components: {
    positions,
    material: () => import('@/views/market/marketManage/createMarkerOrder/components/material'),
    numberCode: () => import('@/components/NumberCode')
  },
  mixins: [autocomplete, bility],
  data() {
    return {
      essential: 'information',
      fnumber: '',
      isMateria: false,
      aotuTab: false,
      isDisabled: false,
      isPositions: false,
      // 表头数据
      tableData: [{
        materialfnumber: '',
        fmodel: '',
        fspecificaTion: '',
        fdescripTion: '',
        funitName: '',
        fqty: 0,
        fentryid: 0,
        foutcode: '',
        positionnumber: '',
        codes: []
      }],
      tableHeader: [
        { label: this.$t('market.fmaterial'), type: 'bill', prop: 'materialfnumber', width: '150px' },
        { label: this.$t('market.fmodel'), prop: 'fmodel' },
        { label: this.$t('shop.fname2'), prop: 'materialfname' },
        { label: this.$t('shop.fspecificaTion'), prop: 'fspecification' },
        { label: this.$t('market.funit'), prop: 'unit', width: '80' },
        { label: this.$t('market.allotQty'), type: 'tag', prop: 'fqty', width: '80' },
        { label: this.$t('market.outcode'), type: 'state', prop: 'foutcode', width: '150' },
        { label: this.$t('market.storageQty'), prop: 'fcodeQty', width: '80' },
        { label: this.$t('market.incode'), type: 'states', prop: 'positionnumber', width: '150' },
        { label: this.$t('market.fdescripTion'), prop: 'fdescripTion', minWidth: '200px' }
      ],
      optionValue: {
        fdocumentstatus: 'A',
        fdate: new Date().toLocaleDateString().replaceAll('/', '-'),
        fremark: ''
      },
      tableHeight: '',
      indexSelf: 0
    }
  },
  created() {
    this.tableHeight = window.innerWidth > 1440 ? 'calc(100vh - 250px)' : '400px'
  },
  methods: {
    // 监听库位 库位切换
    handelCode(index) {
      const item = this.tableData[index]
      item.codes.forEach(res => {
        if (item.foutcode === res.fnumber) {
          item.fcodeQty = res.fqty
        }
      })
    },
    // 输入物料编码
    getAtuo(index, fnumber) {
      this.indexSelf = index
      this.aotuTab = true
      var vm = this
      clearTimeout(vm.timer) // 清除未执行的代码，重置回初始化状态
      vm.timer = setTimeout(() => {
        this.fnumber = fnumber
      }, 600)
    },
    // 保存
    handleAdd() {
      this.details = []
      const data = {
        ...this.optionValue,
        entitys: this.tableData
      }
      insertTransfer(data).then(res => {
        const { code, message } = res
        if (code === 0) {
          // 关闭当前页面
          this.$store.dispatch('tagsView/guanbi', this.$route)
          this.$router.push({ path: `/shiftingDetail/${res.data}` })
          this.$message({ showClose: true, message: res.message, type: 'success' })
        } else {
          this.$confirm(message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 打开物料弹窗
    handleGetMateriel(row, index) {
      this.isMateria = true
      this.indexSelf = index
    },
    // 物料数据
    materialData(item) {
      if (item.fmaterialid || item.fmaterialId) {
        const items = this.tableData[this.indexSelf]
        items.fmaterialid = item.fmaterialid || item.fmaterialId
        items.materialfnumber = item.fnumber
        items.materialfname = item.fname
        items.fdescripTion = item.fdescripTion
        items.fspecification = item.fspecificaTion
        items.funitid = item.funitId
        items.unit = item.funit
        items.fmodel = item.fmodel
        this.queryTBdCode(item.fnumber)
      }
      this.isMateria = false
    },
    handlePosition(index) {
      this.isPositions = true
      this.indexSelf = index
    },
    // 仓位
    positionData(item) {
      if (item.fid) {
        const items = this.tableData[this.indexSelf]
        items.finpositionid = item.fid
        items.positionnumber = item.fname
      }
      this.isPositions = false
    },
    // 获取库位codes
    queryTBdCode(ev) {
      const items = this.tableData[this.indexSelf]
      const DATA = {
        fnumber: ev
      }
      queryTBdCodeListByFnumber(DATA).then(res => {
        if (res.code === 0) {
          items.codes = res.data
          items.foutcode = res.data[0].fnumber
          items.fcodeQty = res.data[0].fqty
        }
      })
    },
    // 单击行
    rowClick(row) {
      this.indexSelf = row.index
    },
    // 新增 删除行
    twoLine(ev) {
      const data = {
        materialfnumber: '',
        fmodel: '',
        fspecificaTion: '',
        fdescripTion: '',
        funitName: '',
        fqty: 0,
        fentryid: 0,
        foutcode: '',
        positionnumber: '',
        codes: []
      }
      if (ev === 1) {
        this.tableData.push(data)
      } else {
        this.tableData.splice(this.indexSelf, 1)
      }
    },
    // 点击单元格
    cellClick(row, column, cell, event) {
      this.property = column.property
      this.subscript = row.index
    },
    // 批量填充
    batchFill() {
      const item = this.tableData[this.subscript][this.property]
      this.tableData.map((res, index) => {
        if (index >= this.subscript) {
          res[this.property] = item
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.layout {
  @include listBom;
  padding: 10px;
  background: #FFFFFF;
  .tabPosit {
    padding: 5px;
    box-shadow: 0 0 2px #aaa;
    position: absolute;
    top: 3.5vh;
    left: 0;
    background-color: rgba(255, 255, 255, 1);
    z-index: 99999!important;
  }
  .tabPosit1 {
    padding: 5px;
    box-shadow: 0 0 2px #aaa;
    position: absolute;
    top: -25.69vh;
    left: 0;
    background-color: rgba(255, 255, 255, 1);
    z-index: 9999;
  }
  .el-input::v-deep .el-input__inner {
    height: 30px;
    line-height: 30px;
  }
  .el-form {
    display: flex;
    flex-wrap: wrap;

    .el-form-item {
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      //height: 35px;
      width: 275px;
      margin-bottom: 5px;
      .btnType{
        margin-right: 20px;
      }
      .el-date-editor.el-input{
        width: 100% !important;
      }
      .el-select{
        width: 100%;
      }
    }
  }
}
</style>
